/*
 * AdapterBase.cpp
 *
 *  Created on: Nov 10, 2012
 *      Author: Scorpion
 */

#include "AdapterBase.h"
namespace centny {
namespace file {
const char * MONTHES[MONTHES_SIZE] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun",
		"Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
//
FInfo::FInfo(AdapterBase *cb, FInfo *parent) {
	this->cb = cb;
	this->parent = parent;
	this->type = 0;
	this->mtime = 0;
	this->size = 0;
	this->cwd = "";
	this->name = "";
}
FInfo::~FInfo() {
//	printf("free FInfo\n");
}
vector<FInfo*>& FInfo::subs() {
	if (!this->isDir() || _subs.size()) {
		return _subs;
	}
	assert(this->cb);
	vector<FInfo*> fis;
	fis = this->cb->subs(this);
	vector<FInfo*>::iterator it;
	for (it = fis.begin(); it != fis.end(); it++) {
		this->_subs.push_back(*it);
	}
	return _subs;
}
void FInfo::showInfo() {
	cout << type << "\t" << size << "\t" << mtime << "\t" << name << endl;
}
void FInfo::showAll() {
	this->showInfo();
	vector<FInfo*> subs = this->subs();
	vector<FInfo*>::iterator it;
	for (it = subs.begin(); it != subs.end(); it++) {
		(*it)->showAll();
	}
}
bool FInfo::isDir() {
	return type == 'd' || type == 'D';
}
FInfo* FInfo::mkdir(string name) {
	return cb->mkdir(this, name);
}
void FInfo::fre() {
	delete this;
}
string FInfo::absUrl() {
	return cb->absUrl(this);
}

//
NetFInfo::NetFInfo(NetAdapterBase *cb, NetFInfo *parent) :
		FInfo(cb, parent) {
}
const char* NetFInfo::upload(string lf, string name) {
	return ((NetAdapterBase*) cb)->upload(this, lf, name);
}
const char* NetFInfo::upload(istream& is, string name) {
	return ((NetAdapterBase*) cb)->upload(this, is, name);
}
const char* NetFInfo::download(string of) {
	return ((NetAdapterBase*) cb)->download(this, of);
}
const char* NetFInfo::download(ostream& os) {
	return ((NetAdapterBase*) cb)->download(this, os);
}
//

AdapterBase::AdapterBase() {
}

AdapterBase::~AdapterBase() {
	this->fre();
}
FInfo* AdapterBase::finfo(string path) {
	FInfo* fi = cwd2f[path];
	return fi;
}
void AdapterBase::fre() {
	map<string, FInfo*>::iterator it;
	for (it = this->cwd2f.begin(); it != this->cwd2f.end(); it++) {
		it->second->fre();
	}
	this->cwd2f.clear();
}
void AdapterBase::addToMap(FInfo* fi) {
	cwd2f[fi->cwd] = fi;
}
string AdapterBase::rootUrl() {
	return this->rurl;
}
string NetAdapterBase::absUrl(FInfo* tg) {
	return this->rurl + "/" + tg->cwd;
}
}
} /* namespace centny */
